package exam.web.action;

import java.util.Date;
import java.util.List;

import exam.common.util.NullCheckUtil;
import exam.model.dao.SubQuestionDAO;
import exam.model.entity.TAnswerEntity;
import exam.model.entity.TQuestionEntity;
import exam.model.entity.TSubQuestionEntity;
import exam.model.entity.TUserEntity;
import exam.web.action.base.QuestionActionBase;

public class EditQuestionAction extends QuestionActionBase {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private List<TAnswerEntity> listAnswer;
	
	private List<TSubQuestionEntity> listSubQuestion;
	
	private SubQuestionDAO subQuestDAO = new SubQuestionDAO();
	
	private boolean type;
	
	public String execute() throws Exception {
		String ret = null;
		listSubQuestion = subQuestDAO.selectListSubQuestion();
		if (NullCheckUtil.isNUll(request.getParameter("btsave"))) {
			String questionId = request.getParameter("questionId");
			if (NullCheckUtil.isNUll(questionId)) {
				ret = ERROR;
			} else {
				settQuestionEntity(questionDAO.selectByPk(Long.valueOf(questionId)));
				listAnswer = answerDAO.selectByQuestionId(gettQuestionEntity().getQuestionId());
				request.setAttribute("listAnswer", listAnswer);
				ret = INPUT;
			}
		} else {
			// luu thay doi
			TQuestionEntity tQuestion = questionDAO.selectByPk(gettQuestionEntity().getQuestionId());
			if (NullCheckUtil.isNotNull(tQuestion)) {
				tQuestion.setQuestionContent(gettQuestionEntity().getQuestionContent());
				tQuestion.setQuestionMark(gettQuestionEntity().getQuestionMark());
				tQuestion.setCorrectNum(gettQuestionEntity().getCorrectNum());
				tQuestion.setQuestionType(isType()?1:0);
				tQuestion.setUpdateDate(new Date());
				tQuestion.setSubQuestionEntity(new TSubQuestionEntity(gettQuestionEntity().getSubQuestId()));
				tQuestion.setUserEntity(new TUserEntity(gettQuestionEntity().getUserId()));
				questionDAO.updateQuestion(tQuestion);
				editAnswer(tQuestion);
				addActionMessage(getText("Cập nhật thông tin đề thi thành công"));
			} else {
				addActionError(getText("Cập nhật thông tin đề thi thất bại"));
			}
			ret = SUCCESS;
		}
		return ret;
	}

	private void editAnswer(TQuestionEntity questionEntity) {
		// danh sach noi dung dap an
		String[] answerContents = request.getParameterValues("answerContents");
		String[] answerIds = request.getParameterValues("answerIds");
		TAnswerEntity answerEntity = null;
		for(int i = 0; i < answerContents.length; i++) {
			answerEntity = new TAnswerEntity();
			answerEntity.setAnswerId(Long.valueOf(answerIds[i]));
			answerEntity.setAnswerContent(answerContents[i]);
			answerEntity.setAnswerCorrect("1".equals(request.getParameter("correct_" + i))?1:0);
			answerEntity.setQuestionEntity(questionEntity);
			answerDAO.updateAnswer(answerEntity);
		}
	}
	
	public List<TAnswerEntity> getListAnswer() {
		return listAnswer;
	}

	public void setListAnswer(List<TAnswerEntity> listAnswer) {
		this.listAnswer = listAnswer;
	}

	public boolean isType() {
		return type;
	}

	public void setType(boolean type) {
		this.type = type;
	}
	
	public List<TSubQuestionEntity> getListSubQuestion() {
		return listSubQuestion;
	}

	public void setListSubQuestion(List<TSubQuestionEntity> listSubQuestion) {
		this.listSubQuestion = listSubQuestion;
	}
}
